package com.qb.devstore;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.widget.TextView;

import com.xuexiang.xui.XUI;
import com.xuexiang.xui.utils.StatusBarUtils;

import utils.MyUtil;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XUI.initTheme(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.tv_hello);
        Drawable drawable = this.getResources().getDrawable(R.drawable.qb_logo);
        drawable.setBounds(0, 0, 96,96);
//        tv.setCompoundDrawables(null, drawable, null, null);
        StatusBarUtils.initStatusBarStyle(this, false, 0x00000000);

/*
        int nId = getIntent().getIntExtra("NOTIFICATION_ID",0);
        DownloadUtil dlu = getIntent().getParcelableExtra("DOWNLOAD_UTIL");
        //NotificationUtil nu = getIntent().getParcelableExtra("NOTIFICATION_UTIL");
        MyUtil.showLog("nId",nId);
        MyUtil.showLog("dlu",dlu);
        //MyUtil.showLog("nu",nu);

        new Handler().postDelayed(()-> {
            if (nId == 0) {
                permissionsInit();
            } else {
                MyUtil.showLog("执行取消下载","");
                dlu.call.cancel();
                NotificationUtil nu = new NotificationUtil(this);
                nu.cancelById(nId);
                finish();
            }
        },1);
*/
        permissionsInit();
    }

    public void mainInit() {
        startActivities(new Intent[]{new Intent().setClass(this, ViewActivity.class)});
    }

    // 手动获取权限
    public void permissionsInit() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.setData(Uri.parse("package:" +this.getPackageName()));
            startActivityForResult(intent, 190009);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !this.getPackageManager().canRequestPackageInstalls()) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
            intent.setData(Uri.parse("package:" +this.getPackageName()));
            startActivityForResult(intent, 190008);
        } else {
            mainInit();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyUtil.showLog("requestCode",requestCode);
        if (requestCode == 190009 || requestCode == 190008) {
            permissionsInit();
        }
    }
}